package com.cj.framework.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageView;


/**
 * Created by cj on 2016/5/26.
 * Email:781806481@qq.com
 */
public class CircleImageView extends ImageView {
    private  Bitmap bitmap;

    public CircleImageView(Context context) {
        this(context,null);
    }

    public CircleImageView(Context context, AttributeSet attrs) {
        this(context,attrs,0);
    }

    public CircleImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        bitmap=((BitmapDrawable)getDrawable()).getBitmap();
    }

    /**
     * func：转换图片为圆形图片
     * alg：
     * 1将原图截成正方形位图；
     * 2将正方形位图缩放到指定大小，即圆形图片的大小，得到缩放后的正方形位图
     * 3新建画布与圆形图片的大小一致，并画圆
     * 4设置画笔为SRC_IN模式，用于将画布上的圆替换为圆对应的缩放后正方形位图
     * 5画位图到画布上，返回画布包含的位图
     * note：
     * canvas有四要素：1用于存储像素的位图  2接收画画的画布层  3画笔 4内容  因此canvas创建的时候必须有bitmap
     * 绘制区原点为出去padding后的区域左上角
     * Rect与RectF的区别主要是在于精度问题
     * paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));这句话的意思是集合运算选择，是交集还是补集等
     * @param bitmap:要转换的图片
     * @param radius:圆形图片的半径
     * @return
     */
    private Bitmap getRoundedBitmap(Bitmap bitmap,int radius){
        //set background
        int diameter=radius*2;
        Bitmap output=Bitmap.createBitmap(diameter,diameter, Bitmap.Config.ARGB_8888);
        Canvas canvas=new Canvas(output);
        Paint paint=new Paint(Paint.ANTI_ALIAS_FLAG);
        canvas.drawCircle(radius,radius,radius,paint);

        //get squareBitmap
        int startX,startY,squareWidth,squareHeight;
        int bitmapWidth=bitmap.getWidth(),bitmapHeight=bitmap.getHeight();
        if(bitmapWidth>=bitmapHeight){
            startX=bitmapWidth/2-bitmapHeight/2;
            startY=0;
            squareHeight=squareWidth=bitmapHeight;
        }else{
            startX=0;
            startY=bitmapHeight/2-bitmapWidth/2;
            squareHeight=squareWidth=bitmapWidth;
        }
        Bitmap squareBitmap=Bitmap.createBitmap(bitmap,startX,startY,squareWidth,squareHeight);

        //get scaledbitmap
        Bitmap scaledBitmap;
        if(diameter==squareWidth){
            scaledBitmap=squareBitmap;
        }else{
            scaledBitmap=Bitmap.createScaledBitmap(squareBitmap,diameter,diameter,true);
        }

        //get together;
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        Rect rect=new Rect(0,0,diameter,diameter);
        canvas.drawBitmap(scaledBitmap,rect,rect,paint);

        return output;
    }

    @Override
    protected void onDraw(Canvas canvas) {

        if(bitmap==null){
            return;
        }

        //获取可绘制区的宽高，获取图片半径
        int realWidth=getWidth()-getPaddingLeft()-getPaddingRight();
        int realHeight=getHeight()-getPaddingTop()-getPaddingBottom();
        int radius;
        if(realHeight>=realWidth){
            radius=realWidth/2;
        }else{
            radius=realHeight/2;
        }

        canvas.drawBitmap(getRoundedBitmap(bitmap,radius),0,0,new Paint());
    }

    public void setBitmap(Bitmap bitmap){
        if(bitmap==null){
            return;
        }
        this.bitmap=bitmap;
        invalidate();
    }

}
